package com.myrent.shiro;

import com.myrent.entity.EnterpriseMerchant;
import com.myrent.entity.User;
import com.myrent.service.EnterpriseService;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 企业的认证
 */
@Configuration
public class EnterpriseRealm extends AuthenticatingRealm{

    @Resource
    EnterpriseService service;

    {super.setName("Enterprise");}

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        CustomizedToken emailPasswordToken = (CustomizedToken) token;
        String eShopEmail = emailPasswordToken.getUsername();
        EnterpriseMerchant enterprise = service.getEnterprise(eShopEmail);
        if (enterprise == null) {
            return null;
        }
        ByteSource salt = ByteSource.Util.bytes(enterprise.getSalt());
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(enterprise.getEShopEmail(), enterprise
                .getEShopPassword(), salt,
                getName());
        return info;
    }
}
